package com.linhh.mall.center.web.controller;

import com.github.pagehelper.PageInfo;
import com.linhh.mall.center.api.UserInfoService;
import com.linhh.mall.center.common.pojo.UserInfo;
import com.linhh.mall.center.common.util.HttpResult;
import io.swagger.annotations.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author linhh
 * @Date 2022 10 09 02 35
 **/
@Api(value = "用户管理")
@RestController
@RequestMapping("/api/users")
public class UserInfoController {

    @Reference
    private UserInfoService userInfoService;

    /**
     * 查询所有信息
     * @return
     */
    @GetMapping("/allUser")
    public List<UserInfo> getAllUserInfo(){
        System.out.println("getAllUserInfo");
        return userInfoService.selectAll();
    }


    /**
     * 添加用户信息
     * @param userInfo
     * @return
     */
    @PostMapping("/addUser")
    public int addUserInfo(@RequestBody UserInfo userInfo){
        System.out.println("addUserInfo");
        return userInfoService.addUserInfo(userInfo);
    }

    /**
     * 更新
     * @param userInfo
     * @return
     */
    @PutMapping("/updateUser")
    public int updateUserInfo(@RequestBody UserInfo userInfo){
        return userInfoService.updateUserInfo(userInfo);
    }

    /**
     * 根据id删除信息
     * @param id
     * @return
     */
    @DeleteMapping("/delUserById/{id}")
    public int deleteUserInfo(@PathVariable Long id){
        return userInfoService.deleteUserInfo(id);
    }

    /**
     * 根据用户名查询信息
     * @param username
     * @return
     */
    @GetMapping("/findByUserName")
    public UserInfo findByUsername(@RequestParam(value = "userName",required = false) String username){
        return userInfoService.findByUserName(username);
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "用户人也查询",notes = "接受分页参数pageNum,pageSize")
    @GetMapping("/getPageUserInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "param", name = "pageNum", value = " 第⼏⻚ ", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "param", name = "pageSize", value = " 每⻚显示多少条数据", required = true, dataType = "int")})
    @ApiResponses({
            @ApiResponse(code = 200, message = "条件分⻚查询⽤户成功"),
            @ApiResponse(code = 500, message = "条件分⻚查询⽤户失败,后台服务出现异 常"),
            @ApiResponse(code = 401, message = "代表此操作⽆权限访问"),
            @ApiResponse(code = 400, message = "表示请求参数错误"),
    })
    public HttpResult getPageUserInfo(@RequestParam int pageNum,@RequestParam int pageSize){
        PageInfo<UserInfo> pageInfo = userInfoService.selectPageInfo(pageNum,pageSize);
        return HttpResult.success(pageInfo);
    }

    @ApiOperation(value = "获取用户列表")
    @GetMapping("/userList")
     public HttpResult queryAllCategory(Integer page,Integer size){
        List<UserInfo> userInfos = userInfoService.selectAll();
        PageInfo<UserInfo> userInfoPageInfo = new PageInfo<>(userInfos);
        return HttpResult.success(userInfoPageInfo);
    }


}
